<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
class UploadController extends CommonController
{
    public function index()
    {

        dump('upload');
    }
    public function uploadimg(Request $request)  {
        // 验证文件是否存在
        $request->validate([
            'file' => 'required|file|max:1024', // 最大文件大小为 1MB
        ]);

        // 获取上传的文件
        $file = $request->file('file');

        // 生成唯一的文件名
        $fileName = time().'_'.$file->getClientOriginalName();
//        dump($fileName);
//        exit();
        // 保存文件到指定目录
        $path = $file->storeAs('uploads', $fileName, 'public');

        if ($path) {
            return re_success('上传成功',[
                "name"=>$file->getClientOriginalName(),
                "url"=>Storage::url($path)
                ]);
        } else {
            return re_warning('上传失败',null);
        }
    }

}
